﻿using System;
using System.Collections.Generic;
using System.Linq;
using Todo.Entity.Models;

namespace Todo.Logic.Server
{
    public class UserServer
    {
        private TodoContext _ctx = new TodoContext();

        List<Things> _things = new List<Things>();
        List<Inventory> _inventorys = new List<Inventory>();
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="nickname"></param>
        /// <param name="password"></param>
        /// <param name="userId"></param>
        public void Register(string nickname, string password)
        {
            Users aUser = new Users
            {
                UserName = nickname,
                Password = password,
            };
            _ctx.Users.Add(aUser);
            _ctx.SaveChanges();
        }

        /// <summary>
        /// 判断是否有重复的用户昵称
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public bool JudgeUserNameRepeat(string userName)
        {
            return _ctx.Users.Any(user => user.UserName == userName) ? false : true;
        }

        /// <summary>
        ///  登陆
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public Users Login(string userName, string password)
        {
            return _ctx.Users.SingleOrDefault
                (user => user.UserName.Equals(userName) && user.Password.Equals(password)); ;
        }

    }
}
